export default function({ $axios, app }) {
  $axios.defaults.timeout = 10000
  $axios.onResponse(response => {
    let data = response.data
    console.log()
    if (data.status >= 200 && data.status < 300) {
      return data
    } else {
      let error = new Error(data.message)
      error.response = response
      return Promise.reject(error)
    }
  })
  $axios.onResponseError(error => {
    let { message } = error
    if (!error.response) {
      if (message.indexOf('timeout') >= 0) {
        message = '网络请求超时!'
      } else {
        message = '网络发生错误!'
      }
    } else {
      let { status, data } = error.response
      if (status === 401) {
        message = data.message
      }
    }
    return Promise.reject(new Error(message))
  })
}
